package org.komarichin.servlets.upload;

import org.apache.commons.lang.StringUtils;
import org.komarichin.beans.IFile;

/**
 * <p>
 * format for xml <br/>
 * example for file:<br/>
 * <b>
 * &lt;file name=&quot;ex.png&quot; id=&quot;4324&quot; length=&quot;123456&quot; contentType=&quot;image/png&quot; isDirectory=&quot;false&quot; path=&quot;/img&quot;/&gt;
 * </b><br/>
 * example for directory<br/>
 * <b>
 * &lt;file name=&quot;/image&quot; id=&quot;4324&quot;  isDirectory=&quot;true&quot; /&gt;
 * </b>
 * </p>
 * <p>
 * format for json<br/>
 * example for file: <br/>
 * <b>
 * {name:'ex.png', id: '4324', length:'12345', contentType:'image/png', isDirectory:'false', path:'/img'}
 * </b><br/>
 * example for directory<br/>
 * <b>
 * {name: '/image', id: '4324', isDirectory: 'true'}
 * </b>
 * <p/>
 * </p>
 * User: Mykhaylo Komarichyn
 * Date: 04.01.2010
 * Time: 14:24:12
 */
public class ResponseFile {
    private Long id;
    private String name;
    private Long length;
    private String contentType;
    private boolean isDirectory;
    private String path;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getLength() {
        return length;
    }

    public void setLength(Long length) {
        this.length = length;
    }

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    public boolean isDirectory() {
        return isDirectory;
    }

    public void setDirectory(boolean directory) {
        isDirectory = directory;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public void wrap(IFile<Long> file) {
        this.name = file.getName();
        this.contentType = file.getContentType();
        this.length = file.getLength();
        this.id = file.getId();
        this.path = file.getFolder();
        this.isDirectory = file.isDirectory();
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("ResponseFile");
        sb.append("{id=").append(id);
        sb.append(", name='").append(name).append('\'');
        sb.append(", length=").append(length);
        sb.append(", contentType='").append(contentType).append('\'');
        sb.append(", isDirectory=").append(isDirectory);
        sb.append(", path='").append(path).append('\'');
        sb.append('}');
        return sb.toString();
    }

    public String toXML() {
        if (StringUtils.isNotEmpty(name)) {
            return StringUtils.EMPTY;
        }
        StringBuilder builder = new StringBuilder();

        builder.append("<file name=\"").append(name).append("\" ")
                .append("id=\"").append(id).append("\" ")
                .append("isDirectory=\"").append(isDirectory).append("\" ")
                .append("path=\"").append(path).append("\" ");
        if (isDirectory()) {
            builder.append("/>");
        } else {
            builder.append("contentType=\"").append(contentType).append("\" ")
                    .append("length=\"").append(length).append("\"/>");
        }
        return builder.toString();
    }

    public String toJSON() {
        if (StringUtils.isNotEmpty(name)) {
            return StringUtils.EMPTY;
        }
        StringBuilder builder = new StringBuilder("{");

        builder.append("name:'").append(name).append("',")
                .append("id:'").append(id).append("',")
                .append("isDirectory:'").append(isDirectory).append("',")
                .append("path:'").append(path).append("'");
        if (isDirectory()) {
            builder.append('}');
        } else {
            builder.append("contentType:'").append(contentType).append("',")
                    .append("length:'").append(length).append("'}");
        }
        return builder.toString();
    }
}
